//
//  AppDelegate.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/9.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        
        //1.创建window
       window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //2.设置控制器
//        let MainVC = XTMainTabBarController()
//        window?.rootViewController = MainVC
        
        
        //测试
        //判断是否登录过
        window?.rootViewController = isLogin()
        
        
        
        
        
        //3.设置主窗口
        window?.makeKeyAndVisible()
        
        //设置导航栏皮肤
        setUpAppearance()
        
        
        //解档--获得账号
//       let userAccount = XTUserAccountViewModel.sharedInstance.userAccount
//        XTPrint(items: "userAccount : \(userAccount)")
        
        return true
    }
    
    //转换根控制器
    func changeRootViewController(rootVC : UIViewController) {
    
        window?.rootViewController = rootVC
    }
    
    
    
    //判断是否登录过
    private func isLogin() -> UIViewController {
        //解档,拿到有AccessToken的那个全局的属性
       let isLogin = XTUserAccountViewModel.sharedInstance.isLogin
//       XTPrint(items: "XTUserAccountViewModel.sharedInstance : \(XTUserAccountViewModel.sharedInstance.userAccount)")
        //判断全局属性有没有值,
        //有值表示已经登录,没有值表示账号过期或者没有登录
        if isLogin == true {
            //已经登录
            XTPrint(items: "已经登录过了")
            //判断是不是新版本
           return isNewVersion()
            
        }else {
            //没有登录 或者 账号过期
            XTPrint(items: "账号过期或者没有登录")
            return XTMainTabBarController()
        }
    
    }
    
    
    //判断是否是新版本
   private func isNewVersion() -> UIViewController {
        
        //拿到当前版本
       let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //获取上一个版本(版本号保存在哪里?--偏好设置)
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        //跟上一个版本比较
        //如果不同就是新版本,(版本只会升)
        if currentVersion != sandboxVersion {
            //版本不同将版本信息存入沙盒中
            NSUserDefaults.standardUserDefaults().setValue(currentVersion, forKey: sandboxVersionKey)
            return XTNewFeatureCollectionViewController()
        }
    
        return XTWelcomViewController()
    }
    
    
    //设置导航栏的皮肤
   private func setUpAppearance() {
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

